package com.jzr.open.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.open.db1.model.TShiftPlan;
import com.jzr.open.db1.service.ShiftPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/shiftPlan")
public class ShiftPlanController {
    @Autowired
    private ShiftPlanService shiftPlanService;

    @PostMapping("/get")
    public Result<List<TShiftPlan>> get(@RequestBody TShiftPlan record){
        if(U.isBlank(record.getDeptCode())){
            return Result.fail("deptCode不能为空");
        }
        QueryWrapper<TShiftPlan> qw = new QueryWrapper<>();
        qw.orderByAsc("dept_code","staff_type","staff_no");

        if(U.isNotBlank(record.getDeptCode())) {
            qw.eq(TShiftPlan.DeptCode_,record.getDeptCode());
        }
        if(U.isNotBlank(record.getStaffType())){
            qw.eq(TShiftPlan.StaffType_,record.getStaffType());
        }
        if(U.isNotBlank(record.getStaffNo())){
            qw.eq(TShiftPlan.StaffNo_,record.getStaffNo());
        }
        if(U.isNotBlank(record.getY())){
            qw.eq(TShiftPlan.Y_,record.getY());
        }
        if(U.isNotBlank(record.getM())){
            qw.eq(TShiftPlan.M_,record.getM());
        }
        List<TShiftPlan> list = shiftPlanService.list(qw);
        return Result.success("成功",list,list==null ? 0:list.size());
    }

    @PostMapping("/post")
    public Result<Integer> post(@RequestBody List<TShiftPlan> list){
        if(list == null || list.isEmpty()){
            return Result.fail("数据不能为空");
        }
        for(TShiftPlan record : list){
            if(U.isBlank(record.getDeptCode())){
                return Result.fail("deptCode不能为空");
            }
            if(U.isBlank(record.getStaffType())){
                return Result.fail("staffType不能为空");
            }
            if(U.isBlank(record.getStaffNo())){
                return Result.fail("staffNo不能为空");
            }
            if(U.isBlank(record.getY())){
                return Result.fail("年份y不能为空");
            }
            if(U.isBlank(record.getY())){
                return Result.fail("月份m不能为空");
            }
        }
        int count = shiftPlanService.save(list);
        if(count <= 0){
            return Result.fail("保存失败");
        }else {
            return Result.success("成功",count,count);
        }
    }
//    @PostMapping("/delete")
//    public Result<Integer> delete(@RequestBody List<TShiftPlan> list){
//        if(list == null || list.isEmpty()){
//            return Result.fail("数据不能为空");
//        }
//        for(TShiftPlan record : list){
//            if(U.isBlank(record.getStaffType())){
//                return Result.fail("staffType不能为空");
//            }
//            if(U.isBlank(record.getStaffNo())){
//                return Result.fail("staffNo不能为空");
//            }
//            if(U.isBlank(record.getY())){
//                return Result.fail("年份y不能为空");
//            }
//            if(U.isBlank(record.getY())){
//                return Result.fail("月份m不能为空");
//            }
//        }
//        int count = shiftPlanService.delete(list);
//        if(count <= 0){
//            return Result.fail("删除失败");
//        }else {
//            return Result.success("成功",count);
//        }
//    }
}
